package com.essence.grabiec104.grab.model;

import lombok.Data;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 时间范围模型
 */
@Data
public class TimeRange {
    /**
     * 开始时间戳
     */
    private long startTime;
    
    /**
     * 结束时间戳
     */
    private long endTime;
    
    /**
     * 月份描述（例如：2024-03）
     */
    private String monthDesc;

    /**
     * 计算指定月份数的时间范围
     *
     * @param cycle 循环次数（月份数）
     * @return 时间范围列表
     */
    static public List<TimeRange> CalculateMonthlyRanges(int cycle) {
        List<TimeRange> timeRanges = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();

        for (int i = 0; i < cycle; i++) {
            TimeRange timeRange = new TimeRange();

            // 设置月份描述
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
            String monthDesc = now.format(formatter);
            timeRange.setMonthDesc(monthDesc);

            // 设置月初时间戳
            LocalDateTime startOfMonth = now.withDayOfMonth(1)
                    .withHour(0)
                    .withMinute(0)
                    .withSecond(0)
                    .withNano(0);
            timeRange.setStartTime(startOfMonth.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());

            // 设置月末时间戳
            LocalDateTime endOfMonth = now.withDayOfMonth(now.toLocalDate().lengthOfMonth())
                    .withHour(23)
                    .withMinute(59)
                    .withSecond(59)
                    .withNano(999999999);
            timeRange.setEndTime(endOfMonth.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());

            timeRanges.add(timeRange);

            // 移动到上个月
            now = now.minusMonths(1);
        }

        return timeRanges;
    }

    static public List<TimeRange> CalculateDailyRanges(int cycle) {
        List<TimeRange> timeRanges = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();

        for (int i = 0; i < cycle; i++) {
            TimeRange timeRange = new TimeRange();

            // 设置日期描述
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            String dayDesc = now.format(formatter);
            timeRange.setMonthDesc(dayDesc);

            // 设置当天开始时间戳
            LocalDateTime startOfDay = now.withHour(0)
                    .withMinute(0)
                    .withSecond(0)
                    .withNano(0);
            timeRange.setStartTime(startOfDay.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());

            // 设置当天结束时间戳
            LocalDateTime endOfDay = now.withHour(23)
                    .withMinute(59)
                    .withSecond(59)
                    .withNano(999999999);
            timeRange.setEndTime(endOfDay.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());

            timeRanges.add(timeRange);

            // 移动到前一天
            now = now.minusDays(1);
        }

        return timeRanges;
    }

    static public List<TimeRange> CalculateWeeklyRanges(int cycle) {
        List<TimeRange> timeRanges = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();

        for (int i = 0; i < cycle; i++) {
            TimeRange timeRange = new TimeRange();

            // 设置周描述
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            String weekStart = now.with(java.time.temporal.TemporalAdjusters.previousOrSame(java.time.DayOfWeek.MONDAY))
                    .format(formatter);
            String weekEnd = now.with(java.time.temporal.TemporalAdjusters.nextOrSame(java.time.DayOfWeek.SUNDAY))
                    .format(formatter);
            timeRange.setMonthDesc(weekStart + " ~ " + weekEnd);

            // 设置周开始时间戳
            LocalDateTime startOfWeek = now.with(java.time.temporal.TemporalAdjusters.previousOrSame(java.time.DayOfWeek.MONDAY))
                    .withHour(0)
                    .withMinute(0)
                    .withSecond(0)
                    .withNano(0);
            timeRange.setStartTime(startOfWeek.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());

            // 设置周结束时间戳
            LocalDateTime endOfWeek = now.with(java.time.temporal.TemporalAdjusters.nextOrSame(java.time.DayOfWeek.SUNDAY))
                    .withHour(23)
                    .withMinute(59)
                    .withSecond(59)
                    .withNano(999999999);
            timeRange.setEndTime(endOfWeek.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());

            timeRanges.add(timeRange);

            // 移动到上一周
            now = now.minusWeeks(1);
        }

        return timeRanges;
    }
} 